package com.atguigu.gulimall.search.thread;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.*;

public class TheadTest {
    private static ExecutorService executorService = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 1.继承Thread类
//        System.out.println("main方法start、、、、、、、");
//        Thread0 thread0 = new Thread0();
//        thread0.start();
//        System.out.println("main方法end、、、、、、、");
        // 2.实现Runnable接口
//        System.out.println("main方法start、、、、、、、");
//        Thread1 thread1 = new Thread1();
//        new Thread(thread1).start();
//        System.out.println("main方法end、、、、、、、");
        // 3.实现Callable接口 + FutureTask （可以拿到返回结果，可以处理异常）
//        System.out.println("main方法start、、、、、、、");
//        FutureTask<Integer> integerFutureTask = new FutureTask<>(new Callable01());
//        new Thread(integerFutureTask).start();
//        try {
//            //阻塞等待拿结果
//            Integer integer = integerFutureTask.get();
//            System.out.println("Callable返回值："+integer);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
//        System.out.println("main方法end、、、、、、、");

        //4.线程池：给线程池直接提交任务
        // 一个系统最好只有一到两个线程池。
//        System.out.println("main方法start、、、、、、、");
//        executorService.execute(new Thread0());
//        System.out.println("main方法end、、、、、、、");
//        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
//                5,
//                200,
//                10,
//                TimeUnit.SECONDS,
//                new LinkedBlockingDeque<>(100000),
//                Executors.defaultThreadFactory(),
//                new ThreadPoolExecutor.DiscardOldestPolicy()
//        );


        // java8 异步CompletableFuture
        System.out.println("main方法start、、、、、、、");
        //1.没有返回的异步
//        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10/2;
//            System.out.println("运行结果：" + i);
//        }, executorService);
        //2.有返回值的异步，并且可以回调
//        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 5;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executorService).whenComplete((result,exce) -> {
//            System.out.println("得到的返回值：" + result);
//            System.out.println("得到的异常：" + exce);
//        }).exceptionally(throwable -> {
//            return 10;
//        });
//        Integer integer = integerCompletableFuture.get();
//        System.out.println("得到的返回值：" + integer);
        //3.有返回值的异步，并且可以回调，还能处理异常。
//        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executorService).handle((res,exce) -> {
//            if (res != null) {
//                return res*2;
//            }
//            if (exce != null) {
//                return 0;
//            }
//            return -1;
//        });
//        Integer integer = integerCompletableFuture.get();
//        System.out.println("得到的返回值：" + integer);
        //穿行调用
//        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executorService).thenApplyAsync((res) -> {
//            System.out.println("任务2启动了");
//            return res;
//        },executorService);

//        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executorService);
//
//        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            String str = "hello";
//            System.out.println("hello：" + str);
//            return str;
//        }, executorService);
//        CompletableFuture<String> stringCompletableFuture = future1.thenCombineAsync(future2, (f1, f2) -> {
//            System.out.println(f1 + f2);
//            return f1 + f2;
//        });
//        String s = stringCompletableFuture.get();

        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("获取图片信息");
            return "hello.jpg";
        },executorService);

        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("获取商品属性");
            return "黑色";
        },executorService);

        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(5000);
                System.out.println("获取详细");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "华为";
        },executorService);
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
        voidCompletableFuture.get();
        System.out.println("main方法end、、、、、、、");
    }

    public static class Thread0 extends Thread {
        @Override
        public void run() {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10/2;
            System.out.println("运行结果：" + i);
        }
    }

    public static class Thread1 implements Runnable {

        @Override
        public void run() {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10/2;
            System.out.println("运行结果：" + i);
        }
    }

    public static class Callable01 implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10/2;
            System.out.println("运行结果：" + i);
            return i;
        }
    }
}
